<?php
/**
 * 命令行具体执行程序
 * 1)必须放置于cli/script目录下
 * 2)文件名必须为小写的cli_xxxx.php
 * 3)类名必须为Cli_Xxxx格式
 * 4)必须包含一个run($args)方法,$args为自动转换后的命令行参数
 *
 * 调用方法:
 * <code>
 * cli script=demo username=sharmy
 * </code>
 *
 * 命令行应用示例
 *
 * @author Chengtian.Hu<chengtian.hu@gmail.com>
 */

class Cli_expsms{
    public function run($args){
    		
    		set_time_limit(0);
    		$begin = strtotime(date('Y-m-d'));
    		$end = $begin+86400;
    		$agents = Acct::find('expsms = 1')->setColumns('id,acctname,smsurl,expmsg')->asArray()->getAll();
    		
    		if (count($agents))
    		{
    		    //$agents = Helper_Array::toHashmap($agents, 'id','acctname');
    		    foreach ($agents as $k=>$v)
    		    {
    		        $users = Acctuser::find('expireddate=? and enable = 1 and agent_id = ?',date('Y-m-d',time()+86400),$v['id'])->setColumns('id,acctname,expireddate')->asArray()->getAll();
    		        
    		        foreach ($users as $item)
    		        {
    		            $msg = $v['expmsg'];
    		            $msg = urlencode($msg);
    		            $url = str_replace('%m%', $item['acctname'], $v['smsurl']);
					    $url = str_replace('%s%', $msg, $url);
					    $result = Helper_Common::cget($url);
					    Helper_Common::log($v['acctname'].'发送过期短信到'.$item['acctname'],'expmsg');
					    Syslog::notloginlogs('代理'.$v['acctname'].'发送过期短信给用户'.$item['acctname']);
					    QDB::getConn()->execute("call smscost({$v['id']},0.05)");
					    QDB::getConn()->close();
					    QDB::getConn()->connect();
    		        }
    		    }
    		}
    		
    }
}